package leading;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class RegisterTest {
	public static void main(String[] args) throws Exception {
		topTitle();
		int in = 0;
		System.out.println("请您选择要进入的版块(输入版块对应的数字)");
		Scanner scanner = new Scanner(System.in);
		in = scanner.nextInt();
		while(in!=1&&in!=2&&in!=3&&in!=4&&in!=5&&in!=6){
			System.out.println("您输入的版块号有误，请重新输入：");
			Scanner tscanner = new Scanner(System.in);
			in = tscanner.nextInt();
		}
		//注册
		if(in == 1){
			register();
		}
		//登入
		if(in == 2){
			load();
		}
		
	}
	//注册
	private static void register(){
		File file;
		String name = "";
		String password = "";
		String rPassword = "";
		System.out.println("欢迎注册成为其中一员！");
		System.out.println("请您输入用户名：");
		Scanner rScanner = new Scanner(System.in);
		name = rScanner.next();
		file = new File("D:\\chewl\\"+name+".txt");
		
		//存在相同文件名说明已经有人注册过了
		while(file.exists()){
			System.out.println("该用户已被注册！");
			Scanner trScanner = new Scanner(System.in);
			name = trScanner.next();
			file = new File("D:\\chewl\\"+name+".txt");
		}
		
		System.out.println("请您输入密码:");
		Scanner prScanner = new Scanner(System.in);
		password = prScanner.next();
		System.out.println("请您确认密码：");
		Scanner rprScanner = new Scanner(System.in);
		rPassword = rprScanner.next();
		while(!(password .equals(rPassword))){
			System.out.println("两次输入的密码不一样!重新输入确认密码!");
			Scanner rprScanner1 = new Scanner(System.in);
			rPassword = rprScanner1.next();
		}
		
		//写入文件,文件名为用户名
		try {
			file.createNewFile();
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			bw.write(name+"\n");
			bw.write(password);
			//不关的话写不进去啊！
			bw.close();
			System.out.println("恭喜啦！注册成功！");
			topTitle1();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	//登入
	private static void load() throws Exception{
		Scanner lScanner;
		File lFile;
		String lName ="";
		String lPass = "";
		String readName = "";
		String readPass = "";
		int num = 1;
		System.out.println("您要登入,请输入用户名：");
		lScanner = new Scanner(System.in);
		lName = lScanner.next();
		System.out.println("请输入密码：");
		lScanner = new Scanner(System.in);
		lPass = lScanner.next();
		
		//读文件
		lFile = new File("D:\\chewl\\"+lName+".txt");
		while(!lFile.exists()){
			System.out.println("您输入的用户名不存在！重新输入！");
			lScanner = new Scanner(System.in);
			lName = lScanner.next();
			lFile = new File("D:\\chewl\\"+lName+".txt");
		}
		try {
			BufferedReader lBr = new BufferedReader(new FileReader(lFile));
			String strr = null;
			while((strr =lBr.readLine())!=null){
				if(num==1){
					readName = strr;
					System.out.println("`````````````"+readName);
				}
				if(num==2){
					readPass = strr;
					System.out.println("`````````````"+readPass);
				}
				num ++ ;
			}
			lBr.close();
			while(!lPass.equals(readPass)){
				System.out.println("您输入的密码有误！重新输入！");
				lScanner = new Scanner(System.in);
				lPass = lScanner.next();
			}
			
			System.out.println("恭喜您登入成功！！");
			topTitle1();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private static void topTitle(){
		System.out.println("********欢迎进入XXX的博客**********");
		System.out.println();
		System.out.println("1、注册成为会员                2、会员登入 ");
		System.out.println();
		System.out.println("3、进入相册                           4、进入留言版");
		System.out.println();
		System.out.println("5、看其他人说什么            6、进入主人日志");
	}
	
	private static void topTitle1(){
		System.out.println("********欢迎进入XXX的博客**********");
		System.out.println();
		System.out.println("1、欢迎您的到来                2、进入主人日志 ");
		System.out.println();
		System.out.println("3、进入相册                           4、进入留言版");
		System.out.println();
		System.out.println("5、看其他人说什么            6、安全退出");
	}
}